package com.csxy.ljr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.ljr.R;
import com.csxy.ljr.entity.EduCourse;
import com.csxy.ljr.entity.vo.CourseInfoForm;
import com.csxy.ljr.entity.vo.CoursePublishVo;
import com.csxy.ljr.entity.vo.CourseQuery;
import com.csxy.ljr.entity.vo.CourseWebVo;
import com.csxy.ljr.entity.vo.EduCourseVo;
import com.csxy.ljr.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Tag(name = "course", description = "课程相关接口")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    // 这里可以添加更多的课程相关接口方法，例如添加课程、查询课程等
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        // 根据前端页面去做的表单实体类，用于接收前端传来的对象
        String id =eduCourseService.addCourseInfo(courseInfoForm);
        //回传课程ID主要是下一步创建章节时候，把课程和章节关联起来
        return R.ok().data("courseId", id);
    }

    //查询课程信息列表
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId) {
        // 根据课程ID查询课程信息
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfoById(courseId);
        return R.ok().data("courseInfoForm", courseInfoForm);
    }

    //查询课程信息列表
    @GetMapping("/getCourseList/")
    public R getCourseListWithSlash() {
        // 这里可以添加逻辑来查询课程列表
        // 例如调用 eduCourseService.getAllCourses() 方法
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }

    //查询课程信息列表
    @GetMapping("/getCourseList")
    public R getCourseList() {
        // 这里可以添加逻辑来查询课程列表
        // 例如调用 eduCourseService.getAllCourses() 方法
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }
//    修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }
    @DeleteMapping("/removeCourseById/{id}")
    public R removeCourseById(@PathVariable String id) {
        boolean flag = eduCourseService.removeCourseById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    
    // 多条件分页查询
    @Operation(description = "多条件查询课程分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(@Parameter(name = "page", description = "当前页码", required = true)
                                @PathVariable Long page,
                                @Parameter(name = "limit", description = "每页记录数", required = true)
                                @PathVariable Long limit,
                                @RequestBody(required = false) CourseQuery courseQuery) {
        // 创建分页page对象
        Page<EduCourse> pageParam = new Page<>(page, limit);
        // 调用方法实现多条件分页查询
        eduCourseService.pageQuery(pageParam, courseQuery);
        // 获取查询到的数据
        List<EduCourse> records = pageParam.getRecords();
        // 获取总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
    @GetMapping("/getpublishCourseInfo/{id}")
    public R getpublishCourseInfo(@PathVariable String id) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", publishCourseInfo);
    }
    
    // 课程最终发布
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        boolean result = eduCourseService.publishCourse(id);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    
    // 根据课程id，查询课程确认信息【订单】
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable String courseId) {
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(courseId);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(courseInfo, eduCourseVo);
        return eduCourseVo;
    }
}